package edu.its.dreamteam.web.pages.admin;

import java.util.ArrayList;
import java.util.List;

import org.apache.tapestry.ComponentResources;
import org.apache.tapestry.annotations.Retain;
import org.apache.tapestry.beaneditor.BeanModel;
import org.apache.tapestry.ioc.annotations.Inject;
import org.apache.tapestry.services.BeanModelSource;

import edu.its.dreamteam.daf.beans.DraftPlayer;
import edu.its.dreamteam.daf.beans.Manager;
import edu.its.dreamteam.daf.beans.Team;
import edu.its.dreamteam.daf.beans.WeeklyResult;
import edu.its.dreamteam.daf.dao.IDraftPlayerDao;
import edu.its.dreamteam.daf.dao.ITeamDao;
import edu.its.dreamteam.daf.dao.IWeeklyResultDao;
import edu.its.dreamteam.web.base.SecureBasePage;

public class TeamGrid extends SecureBasePage {
	@Inject
	private ITeamDao teamDao;
	@Inject
	private ITeamDao managerDao;
	@Inject
	private IWeeklyResultDao weeklyResultDao;
	@Inject
	private IDraftPlayerDao draftPlayerDao;

	private List<WeeklyResult> weeklyResultList;
	private List<DraftPlayer> draftPlayerList;
	private List<Manager> managerList;
	private List<Team> teamList;
	private Team team;

	@Retain
	private BeanModel teamModel;
	@Inject
	private BeanModelSource teamModelSource;
	@Inject
	private ComponentResources resources;

	private boolean delete;
	private List<String> deletedList;
	private Team teamManager;
	public Team getTeamManager(){
		if(teamManager==null){
			teamManager = (Team)getTeamDao().queryForBaseBean("\"username\" ='"+getVisit().getUsername()+"' ");
		}
		return teamManager;
	}
	public Team getTeam() {
		return team;
	}

	public void setTeam(Team team) {
		this.team = team;
	}

	public ITeamDao getTeamDao() {
		return teamDao;
	}
	
	public ITeamDao getManagerDao() {
		return managerDao;
	}
	public IWeeklyResultDao getWeeklyResultDao() {
		return weeklyResultDao;
	}

	public IDraftPlayerDao getDraftPlayerDao() {
		return draftPlayerDao;
	}
	
	public List<WeeklyResult> getWeeklyResultList() {
		if(weeklyResultList==null){
			weeklyResultList = getWeeklyResultDao().queryForList("", "");
		}
		return weeklyResultList;
	}

	public void setWeeklyResultList(List<WeeklyResult> weeklyResultList) {
		this.weeklyResultList = weeklyResultList;
	}
	
	public List<Manager> getManagerList() {
		if(managerList==null){
			managerList = getManagerDao().queryForList("", "");
		}
		return managerList;
	}
	public void setManagerList(List<Manager> managerList) {
		this.managerList = managerList;
	}
	public List<DraftPlayer> getDraftPlayerList() {
		if(draftPlayerList==null){
			draftPlayerList = getDraftPlayerDao().queryForList("\"status_draft\" = '1' and \"play\" ='1' ", null);
		}
		return draftPlayerList;
	}

	public void setDraftPlayerList(List<DraftPlayer> draftPlayerList) {
		this.draftPlayerList = draftPlayerList;
	}

	public boolean isDelete() {
		return delete;
	}

	public void setDelete(boolean delete) {
		if (delete) {
			getDeletedList().add(getTeam().getIdTeam());
		}

	}

	public List<String> getDeletedList() {
		if (deletedList == null) {
			deletedList = new ArrayList<String>();
		}
		return deletedList;
	}

	public List<Team> getTeamList() {
		if (teamList == null) {
			teamList = new ArrayList<Team>();
			teamList = getTeamDao().queryForList("", "");
		}
		return teamList;
	}

	public void setTeamList(List<Team> teamList) {
		this.teamList = teamList;
	}

	public BeanModel getTeamModel() {
		return teamModel;
	}

	void pageLoaded() {
		teamModel = teamModelSource.create(Team.class, false, resources);
		teamModel.remove("idTeam");
		teamModel.add("actions", null).label("Edit");
		teamModel.get("idFormation").label("Formation");
		teamModel.reorder("actions");
	}

	void onActionFromTeamValidation() {
		for(WeeklyResult weeklyResultmanager : getWeeklyResultList()){
			for(DraftPlayer draftPlayerManager : getDraftPlayerList()){
				if(weeklyResultmanager.getPlayer().getIdPlayer().equals(draftPlayerManager.getIdPlayer())){
					
					int point = 0;
					
					if(draftPlayerManager.getPlayer().getIdPosition().equals("POST-00001")){
						
						
						if(weeklyResultmanager.getG()>0){
							point+=7;
						}
						if (weeklyResultmanager.getA()>0) {
							point+=5;
						}
						if (weeklyResultmanager.getC()==0) {
							point+=5;
						}
						
						
					}else if(draftPlayerManager.getPlayer().getIdPosition().equals("POST-00002")){
						if(weeklyResultmanager.getG()>0){
							point+=6;
						}
						if (weeklyResultmanager.getA()>0) {
							point+=3;
						}
						if (weeklyResultmanager.getC()==0) {
							point+=4;
						}
						
					}else if(draftPlayerManager.getPlayer().getIdPosition().equals("POST-00003")){
						if(weeklyResultmanager.getG()>0){
							point+=5;
						}
						if (weeklyResultmanager.getA()>0) {
							point+=3;
						}						
					}else if(draftPlayerManager.getPlayer().getIdPosition().equals("POST-00004")){
						if(weeklyResultmanager.getG()>0){
							point+=4;
						}
						if (weeklyResultmanager.getA()>0) {
							point+=3;
						}					
					}
					
					if (weeklyResultmanager.getY()>0) {
						point-=1;
					}
					if (weeklyResultmanager.getC()>0) {
						point-=3;
					}
					Team teamManagerObj = (Team)getTeamDao().findByPrimaryKey(draftPlayerManager.getIdTeam());
					teamManagerObj.setPoint(teamManagerObj.getPoint()+point);
					getTeamDao().update(teamManagerObj);
					
					DraftPlayer draftPlayerObj = (DraftPlayer)getDraftPlayerDao().findByPrimaryKey(draftPlayerManager.getIdDraft());
					draftPlayerObj.setCurrentPlayed(draftPlayerObj.getCurrentPlayed()+1);
					
				}
				
			}
			getWeeklyResultDao().delete(weeklyResultmanager);
		}
		
	}
}
